import java.util.Date;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2022/3/9
 * \* Time: 20:33
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class HashBuck {

    static class Node{
        public int key;
        public int val;
        public Node next;

        public Node(int key,int val){
            this.key = key;
            this.val = val;
        }
    }
        public Node[] array;//定义一个Node类型的数组
        public int usedSize;//定义一个范围值

        public static final  double DOUBLE_LOAD_FATOR = 0.75;//定义一个负载因子，超过这个就扩容

        public HashBuck(){
            this.array = new Node[10];
        }


        public void put(int key,int val){
            //1.通过哈希函数，找到key所在的位置
            int index = key % this.array.length;

            //2.遍历这个index下标的链表，检查有没有相同的key，若有，则更新里面的val值
            Node cur = array[index];
            while(cur !=null){
                if(cur.key == key){
                    cur.val = val;//更新val
                    return;
                }
                cur = cur.next;
            }
            //3.如果没有这个key元素，则用头插法插入，（用尾插法也行）
            Node node = new Node(key,val);
            node.next = array[index];
            array[index] = node;
            this.usedSize++;

            //4.插入元素成功后，检查当前散列表的负载因子,如果大于0.75，则扩容

            if(loadFactor() > DOUBLE_LOAD_FATOR){
                resize();
            }

        }
        private void  resize(){
            Node[] newArray = new Node[array.length*2];//扩容
            //重新计算元素的哈希值
            for (int i = 0;i<array.length;i++){
                Node cur = array[i];
                while (cur != null){
                    int index = cur.key % newArray.length;//获取新的下标
                    //把cur这个结点重新头插到新的数组对应的下标中
                    Node curNext = cur.next;
                    cur.next = newArray[index];
                    newArray[index] = cur;
                    cur = curNext;
                }
            }
            array = newArray;//把array指向newArray,原来的数组就被jvm给回收掉了
        }

        private double loadFactor(){
            return 1.0*usedSize / array.length;
        }

        public int get(int key){
                //1.找到key所在的位置
                int index = key % this.array.length;

                //2.遍历这个下标的列表，看是不是有相同的key，有则更新value值，
                Node cur = array[index];
                while (cur != null){
                    if(cur.key == key){

                        return cur.val;
                    }
                    cur = cur.next;
                }
                return -1;
        }












   /* static class Node{
        public int key;
        public int val;
        public Node next;

        public Node(int key,int val){
            this.key = key;
            this.val = val;
        }
    }

    public Node[] array;
    public int useSize;

    public static final double DEFAULT_LOADFACTOR = 0.75;

    public HashBuck(){
        this.array = new Node[10];
    }

    *//**
     * put函数
     * @param key
     * @param val
     *//*
    public void put(int key,int val){

        //1.找到key所在的位置
        int index = key % this.array.length;

        //2.遍历这个下标的列表，看是不是有相同的key，有则更新value值，
        Node cur = array[index];
        while (cur != null){
            if(cur.key == key){
                cur.val = val;//更新val值
                return;
            }
            cur = cur.next;
        }
        //3.没有这个key，则以头插或者尾插法插进去
        Node node = new Node(key,val);
        node.next = array[index];
        array[index] = node;
        this.useSize++;

        //4.插入元素成功之后，检查当前散列表的负载因子
        if(loadFactor() > DEFAULT_LOADFACTOR){
            resize();
        }

    }

    private void resize(){
        Node[] newArray = new Node[array.length*2];
        for(int i = 0;i<array.length;i++){
            Node cur = array[i];
            while (cur != null){
                int index = cur.key % newArray.length;//获取新的下标
                //把cur这个结点，以头插/尾插的形式，插入到新的数组下标当中
                Node curNext = cur.next;
                cur.next = newArray[index];//先绑定后面
                newArray[index] = cur;//绑定前面
                cur = curNext;

            }
        }
        array = newArray;
    }

    private double loadFactor(){
        return 1.0 * useSize / array.length;
    }

    *//**
     * 根据key获取val值
     * @param
     * @return
     *//*

    public int get(int key){
        //1.找到key所在的位置
        int index = key % this.array.length;

        //2.遍历这个下标的列表，看是不是有相同的key，有则更新value值，
        Node cur = array[index];
        while (cur != null){
            if(cur.key == key){

                return cur.val;
            }
            cur = cur.next;
        }
        return -1;
    }*/

    public static void main(String[] args) {
        HashBuck hashBuck = new HashBuck();
       /* hashBuck.put(1,1);
        hashBuck.put(12,12);
        hashBuck.put(3,3);
        hashBuck.put(7,7);
        hashBuck.put(6,6);
        hashBuck.put(2,2);
        hashBuck.put(11,11);
        System.out.println(hashBuck.get(11));
*/
     //   HashBuck hashBuck = new HashBuck();
        hashBuck.put(12,12);
        hashBuck.put(3,3);
        hashBuck.put(6,6);
        hashBuck.put(2,2);
        hashBuck.put(8,8);
        System.out.println(hashBuck.get(8));

    }

}
